package com.houweishi.module.purchase.controller;

import com.houweishi.common.annotation.Log;
import com.houweishi.common.core.controller.BaseController;
import com.houweishi.common.core.domain.AjaxResult;
import com.houweishi.common.core.page.TableDataInfo;
import com.houweishi.common.enums.BusinessType;
import com.houweishi.common.utils.poi.ExcelUtil;
import com.houweishi.module.purchase.domain.OaPurchase;
import com.houweishi.module.purchase.domain.OaPurchaseApproval;
import com.houweishi.module.purchase.domain.OaPurchaseProduct;
import com.houweishi.module.purchase.service.IOaPurchaseProductService;
import com.houweishi.module.purchase.service.IOaPurchaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 采购申请Controller
 * 
 * @author houweishi
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/module/purchase")
public class OaPurchaseController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(OaPurchaseController.class);
    @Autowired
    private IOaPurchaseService oaPurchaseService;

    @Resource
    private IOaPurchaseProductService oaPurchaseProductService;

    /**
     * 查询采购申请列表
     */
//    @PreAuthorize("@ss.hasPermi('module:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaPurchaseApproval oaPurchase)
    {
        startPage();
        List<OaPurchaseApproval> list = oaPurchaseService.selectOaPurchaseApprovalList(oaPurchase);
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(product->{
                //根据条件查询采购申请产品列表
                selectPurchaseProductList(product);
            });
        }
        return getDataTable(list);
    }

    /**
     * 根据条件查询采购申请产品列表
     *
     * @param oaPurchase
     */
    void selectPurchaseProductList(OaPurchaseApproval oaPurchase) {
        OaPurchaseProduct product = new OaPurchaseProduct();
        product.setPurchaseId(oaPurchase.getId());
        List<OaPurchaseProduct> list = oaPurchaseProductService.selectOaPurchaseProductList(product);
        oaPurchase.setPurchaseProductList(list);
    }

    /**
     * 导出采购申请列表
     */
//    @PreAuthorize("@ss.hasPermi('module:purchase:export')")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaPurchase oaPurchase)
    {
        List<OaPurchase> list = oaPurchaseService.selectOaPurchaseList(oaPurchase);
        ExcelUtil<OaPurchase> util = new ExcelUtil<OaPurchase>(OaPurchase.class);
        util.exportExcel(response, list, "采购申请数据");
    }

    /**
     * 获取采购申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('module:purchase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaPurchaseService.selectOaPurchaseById(id));
    }

    /**
     * 新增采购申请
     */
//    @PreAuthorize("@ss.hasPermi('module:purchase:add')")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaPurchase oaPurchase)
    {
        try {
            return oaPurchaseService.insertOaPurchase(oaPurchase);
        } catch (Exception e) {
            LOGGER.error("新增采购申请失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改采购申请
     */
//    @PreAuthorize("@ss.hasPermi('module:purchase:edit')")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaPurchase oaPurchase)
    {
        return toAjax(oaPurchaseService.updateOaPurchase(oaPurchase));
    }

    /**
     * 删除采购申请
     */
//    @PreAuthorize("@ss.hasPermi('module:purchase:remove')")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oaPurchaseService.deleteOaPurchaseByIds(ids));
    }
}
